import {HTTP} from "../utils/http-p.js";

class KeywordModel extends HTTP{
    key = 'q'
    maxLength = 10
    getHistory(){
        const words = wx.getStorageSync(this.key);
        if(!words){
            return [];
        }
        return words;
    }

    getHot(){
        return this.request({url:'/book/hot_keyword'});
    }

    addToHistory(keyword){
        let words = this.getHistory();
        // includes是es6的，如果不用es6应该就要使用indexOf判断数组是否含有keyword元素
        const has = words.includes(keyword);
        if(!has){//如果历史搜索不包含keyword，则添加到历史搜索
            const length = words.length;
            if(length>=this.maxLength){
                words.pop();//如果历史搜索数组长度过长，则删除数组末尾的元素
            }
            words.unshift(keyword);
            //同步缓存
            wx.setStorageSync(this.key,words);
        }
    }
}

export {KeywordModel}